package yicheng.dinnertogetherapp;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;
import yicheng.dinnertogetherapp.R;



public class FirstLaunchActivity extends Activity {
	private final String photoFileDirectory = android.os.Environment
			.getExternalStorageDirectory()
			+ File.separator
			+ "Eatogether" + File.separator + "myPhoto" + File.separator;
	
	
	private SharedPreferences sharedPref;
	private SharedPreferences.Editor editor;

	private ViewFlipper flipper;
	private Button enterButton;

	private float downXValue;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		//Load shared preference
		sharedPref =  this.getPreferences(Context.MODE_PRIVATE);
		editor = sharedPref.edit();

		//Load firstLaunch data from shared preference
		boolean firstLaunch = sharedPref.getBoolean("firstLaunch", true);


		if (firstLaunch){

			
			
			setContentView(R.layout.first_launch_view);
			
			
			File folder = new File(photoFileDirectory);
			if (!folder.exists()) {
			    folder.mkdirs();
			}
			
			

			//Flipper view set up
			flipper = (ViewFlipper) findViewById(R.id.welcome_view_flipper);
			flipper.setInAnimation(FirstLaunchActivity.this, R.anim.flip_in);
			flipper.setOutAnimation(FirstLaunchActivity.this, R.anim.flip_out);


			enterButton = (Button) findViewById(R.id.welcome_enter_button);

			//Enter button action 
			enterButton.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					flipper.setClickable(false);
					enterButton.setClickable(false);
					flipper.setEnabled(false);

					Handler handler = new Handler(); 
					handler.postDelayed(new Runnable() { 
						public void run() { 
							Intent main_view_intent = new Intent("yicheng.dinnertogetherapp.LOGINACTIVITY");
							startActivity(main_view_intent);
							
						} 
					}, 1500); 
				}
			});


			//Flipper view touch action
			flipper.setOnTouchListener(new View.OnTouchListener() {

				@Override
				public boolean onTouch(View arg0, MotionEvent arg1) {
					// TODO Auto-generated method stub
					switch (arg1.getAction())
					{
					case MotionEvent.ACTION_DOWN:
					{
						// store the X value when the user's finger was pressed down
						downXValue = arg1.getX();
						break;
					}

					case MotionEvent.ACTION_UP:
					{
						// Get the X value when the user released his/her finger
						float currentX = arg1.getX();            


						// going backwards: pushing stuff to the right
						if (65 < currentX - downXValue)
						{
							// Get a reference to the ViewFlipper
							// ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
							// Set the animation
							flipper.setOutAnimation(FirstLaunchActivity.this, R.anim.right_out);
							flipper.setInAnimation(FirstLaunchActivity.this, R.anim.left_in);

							// Flip!
							if (flipper.getDisplayedChild()>= 1 && flipper.getDisplayedChild()<=flipper.getChildCount()-1){
								flipper.showPrevious();
							}
						}


						// going forwards: pushing stuff to the left
						if (downXValue - currentX> 65)
						{
							// Get a reference to the ViewFlipper
							/* ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);*/
							// Set the animation
							flipper.setInAnimation(FirstLaunchActivity.this, R.anim.right_in);
							flipper.setOutAnimation(FirstLaunchActivity.this, R.anim.left_out);

							// Flip!

							if (flipper.getDisplayedChild()>= 0 && flipper.getDisplayedChild()<flipper.getChildCount()-1){
								flipper.showNext();
							}
						}
						break;
					}
					}

					// if you return false, these actions will not be recorded
					return true;
				}
			});


		}
		//If not first launch, then go directly to login activity
		else{
			Intent main_view_intent = new Intent("yicheng.dinnertogetherapp.LOGINACTIVITY");
			
			startActivity(main_view_intent);
		
		}
	}






	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();

		finish();
		
		//Put firstLaunch data into shared preference and change it as not first launch
		editor.putBoolean("firstLaunch", false);
		editor.commit();

	}





}



